﻿using DiverseStudents.Scripts;
using DiverseStudents.UI;
using System;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.UI;

namespace DiverseStudents.Mods
{

    internal class ModsUIListItem : MonoBehaviour, IMosUICellBase
    {
        public RectTransform rectTransform;
        Button button;
        Text modName;
        Text modDesc;
        Toggle modSelect;
        ModArchive modArchive;


        public ModsUIListItem(ModArchive modArchive, int showCount, RectTransform parent, UnityAction action)
        {
            CreateItem(modArchive, showCount, parent, action);
        }
        public void CreateItem(ModArchive mod, int showCount, RectTransform parent, UnityAction action)
        {
            if (parent == null)
            {
                return;
            }
            try
            {
                modArchive = mod;
                var realWidth = parent.rect.width;
                var realHeight = parent.rect.height / showCount;
                button = UICreate.ItemButton(action, height: realHeight, _show: false);

                if (button == null)
                {
                    return;
                }
                rectTransform = button.GetComponent<RectTransform>();
                button.gameObject.AddComponent<ListItemDragScript>();
                button.gameObject.AddComponent<LayoutElement>();
                button.transform.SetParent(parent, false);
                button.gameObject.SetActive(true);
                var hz = button.GetComponent<HorizontalLayoutGroup>();
                hz.childAlignment = TextAnchor.MiddleLeft;
                hz.childForceExpandWidth = true;

                modName = UICreate.Text(modArchive.name, type: UICreate.TextType.Tips, flag: "ItemName");
                modName.gameObject.AddComponent<AspectRatioFitter>().aspectRatio = 3;
                modName.transform.SetParent(rectTransform, false);

                modDesc = UICreate.Text(text: modArchive.description, type: UICreate.TextType.Tips, flag: "ItemDesc");
                modDesc.gameObject.AddComponent<AspectRatioFitter>().aspectRatio = 6;
                modDesc.transform.SetParent(rectTransform, false);

                modSelect = UICreate.DefaultToggle("ItemSelect", modArchive.check, realHeight * 0.5f, realHeight * 0.5f);
                modSelect.onValueChanged.AddListener(OnToggleValueChanged);
                modSelect.gameObject.AddComponent<AspectRatioFitter>().aspectRatio = 1;
                modSelect.transform.SetParent(rectTransform, false);

            }
            catch (Exception e)
            {
                DLog.Error(e);
            }
        }

        public void UpdateData(ModArchive mod)
        {
            modArchive = mod;
            modName.text = modArchive.name;
            modDesc.text = modArchive.description;
            modSelect.isOn = modArchive.check;
        }


        private void OnToggleValueChanged(bool isOn)
        {

        }

        public void UpdateRect(RectTransform baseRect)
        {

        }

        public virtual void NotifyCurrentAssignment()
        {

        }

        public void Destroy()
        {
            if (button != null)
            {
                GameObject.Destroy(button.gameObject);
            }
        }
    }
}
